package i2.application.checklist.entities;

import static javax.persistence.GenerationType.AUTO;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Entity
@Table(name = "livraison")
@NamedQuery(name = "Livraison.findbyIdJoinLivrables", query = "SELECT liv FROM Livraison liv INNER JOIN FETCH liv.livrables WHERE liv.id = :id ")
public class Livraison implements Serializable {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = LoggerFactory.getLogger(Livraison.class);
	
	@Id
	@GeneratedValue(strategy = AUTO)
	@Column(name = "id", unique = true, nullable = false)
	private int id;
	
	@Column(name = "version", nullable = false, length = 45)
	@Size(max=45)
	@NotNull(message="la version est obligatoire")
	@NotEmpty(message="la version ne peut pas être vide")
	private String version;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "application_id", nullable = false)
	@NotNull
	private Application application;
	
	@Column(name = "isok")
	private Boolean isOk = false;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date", nullable = true, length = 19)
	private Date date;
	
	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) //FIXME à refaire  !!!!! ne pas utiliser la cascade ?? et avoir un contrôle plus fin...
	@JoinTable(name = "livrable_has_livraison", joinColumns = { @JoinColumn(name = "livraison_id", nullable = true, updatable = true) }, inverseJoinColumns = { @JoinColumn(name = "livrable_id", nullable = false, updatable = false) })
	@Size(min=1, message="la livraison doit contenir au moins un livrable")
	private List<Livrable> livrables = new ArrayList<Livrable>(0);

	//TODO mettre ça en LAZY
	@OneToOne(mappedBy = "livraison", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	private CheckList checkList;
	
	
	public Livraison() {
	}
	
	public Livraison(int id) {
		this.id = id;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Livraison(Application application, Date date) {
		this.application = application;
		this.date = date;
	}

	public Livraison(Application application, Boolean isOk, Date date,
			List<Livrable> livrables) {
		this.application = application;
		this.isOk = isOk;
		this.date = date;
		this.livrables = livrables;
	}

	public String getVersion() {
		return this.version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public Application getApplication() {
		return this.application;
	}

	public void setApplication(Application application) {
		this.application = application;
	}

	public Boolean isOk() {
		return this.isOk;
	}

	public void setIsOk(Boolean isOk) {
		this.isOk = isOk;
	}

	public Date getDate() {
		return this.date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public List<Livrable> getLivrables() {
		logger.debug("getLivrables : {}",livrables);
		return this.livrables;
	}

	public void setLivrables(List<Livrable> livrables) {
		logger.debug("setLivrables : {}",livrables);
		this.livrables = livrables;
	}
	
	public CheckList getCheckList() {
		return checkList;
	}

	public void setCheckList(CheckList checkList) {
		this.checkList = checkList;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

    @Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Livraison other = (Livraison) obj;
		if (id != other.id) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "Livraison [id=" + id + ", version=" + version
				+ ", application=" + application + ", isOk=" + isOk + ", date="
				+ date + ", livrables=" + livrables + "]";
	}

	public void addLivrable(Livrable livrable) {
		livrables.add(livrable);
	}

	public void deleteLivrable(Livrable livrable) {
		livrables.remove(livrable);
	}
	
	public void deleteLivrable(int index) {
		livrables.remove(index);
	}
    
    
}
